<?php

namespace App\Models\Api;

use App\Models\Common\CollOption;
use Carbon\Carbon;

class Resume extends ApiModel
{
    //表名
    protected $table = 'resume';

    const CREATED_AT = 'regtime';
    const UPDATED_AT = 'updatetime';

    protected $guarded = [];

    /**
     * The attributes that should be hidden for arrays.
     * 隐藏字段
     * @var array
     */
    protected $hidden = ['regtime', 'updatetime', 'uid',
        'homeaddress_area_name', 'jobsort1',
        'jobsort2', 'jobsort3', 'jobsort4', 'jobsort5', 'jobarea1', 'jobarea2',
        'jobarea3', 'jobarea4', 'jobarea5', 'census_area_name', 'default'];

    /**
     * The attributes that should be casted to native types.
     * 转换属性
     * @var array
     */
    protected $casts = [
        'id' => 'string', 'uid' => 'string', 'sex' => 'string', 'job_status' => 'string',
        'resume_status' => 'string', 'bkresume_status' => 'string', 'marital' => 'string',
        'height' => 'string', 'chkphoto_open' => 'string',
    ];

    protected $appends = ['birthday_name'];

    public function resume_workexp()
    {
        return $this->hasMany(ResumeWorkExp::class, 'resume_id')
            ->orderBy('starttime', 'desc');
    }

    public function resume_eduexp()
    {
        return $this->hasMany(ResumeEducationExp::class, 'resume_id')
            ->orderBy('starttime', 'desc');
    }

    public function resume_proexp()
    {
        return $this->hasMany(ResumeProjectExp::class, 'resume_id')
            ->orderBy('starttime', 'desc');
    }

    public function resume_skill()
    {
        return $this->hasMany(ResumeSkill::class, 'resume_id')
            ->orderBy('updatetime', 'desc');
    }

    public function resume_cer()
    {
        return $this->hasMany(ResumeCertificate::class, 'resume_id')
            ->orderBy('gettime', 'desc');
    }

    public function resume_lang()
    {
        return $this->hasMany(ResumeLanguageSkill::class, 'resume_id')
            ->orderBy('updatetime', 'desc');
    }

    //简历作品,暂时没有用
    public function resume_works()
    {
        return $this->hasMany(ResumeWork::class, 'resume_id');
    }

    public function resume_other()
    {
        return $this->hasMany(ResumeOther::class, 'resume_id')
            ->orderBy('updatetime', 'desc');
    }

    /** 年龄
     * @return string
     */
    public function getAgeAttribute()
    {
        return strval(Carbon::parse($this->attributes['birthday'])->age);
    }

    /**
     * 获取学历名
     *
     * @return string
     */
    public function getEducationNameAttribute()
    {
        return CollOption::getOptionName($this->attributes['education'], 'education');
    }

    /**
     * 获取现住址名
     *
     * @return string
     */
    public function getHomeaddressNameAttribute()
    {
        return $this->homeaddress_area_name ? $this->homeaddress_area_name->name : '';
    }

    public function homeaddress_area_name()
    {
        return $this->hasOne(CollArea::class, 'cid', 'homeaddress')
            ->select('cid', 'name');
    }

    /**
     * 获取户籍名
     *
     * @return string
     */
    public function getCensusNameAttribute()
    {
        return $this->census_area_name ? $this->census_area_name->name : '';
    }

    public function census_area_name()
    {
        return $this->hasOne(CollArea::class, 'cid', 'census')
            ->select('cid', 'name');
    }

    /**
     * 公司黑名单
     */
    public function shield()
    {
        return $this->belongsToMany(CompanyUser::class, 'shieldcompanies',
            'resume_id', 'company_uid')->select('id', 'uid', 'company_name');
    }

    /**
     * 获取工作经验名
     *
     * @return string
     */
    public function getWorkYearNameAttribute()
    {
        return CollOption::getOptionName($this->attributes['work_year'], 'work_year');
    }

    /**
     * 获取薪资名
     *
     * @return string
     */
    public function getExpectedsalaryNameAttribute()
    {
        return CollOption::getOptionName($this->attributes['expectedsalary'], 'expectedsalary');
    }

    /**
     * 获取职位id数组
     *
     * @return array
     */
    public function getJobsortAttribute()
    {
        $arr = [];

        //遍历地区
        for ($i = 1; $i <= 5; $i++) {
            $value = $this['jobsort' . $i];
            if ($value != 0) {
                $arr[] = $value;
            }
        }
        return $arr;
    }

    /**
     * 获取职位名
     *
     * @return string
     */
    public function getJobsortNameAttribute()
    {
        $temp = implode(',', \DB::table('coll_position')
            ->whereIn('cid', $this['jobsort'])->pluck('name'));
        return strlen($temp) < 1 ? '不限' : $temp;
    }

    /**
     * 获取地区id数组
     *
     * @return array
     */
    public function getJobareaAttribute()
    {
        $arr = [];

        //遍历地区
        for ($i = 1; $i <= 5; $i++) {
            $value = $this['jobarea' . $i];
            if ($value != 0) {
                $arr[] = $value;
            }
        }
        return $arr;
    }

    /**
     * 获取地区名
     *
     * @return string
     */
    public function getJobareaNameAttribute()
    {
        $area = [];
        $town = [];
        foreach ($this->jobarea as $item) {
            if (strlen(strval($item)) > 6) {
                $town[] = $item;
            } else {
                $area[] = $item;
            }
        }
        $areaName = [];
        if (count($area) > 0) {
            $areaName = CollArea::whereIn('cid', $area)->pluck('name', 'cid')->toArray();
        }
        $townName = [];
        if (count($town) > 0) {
            $townName = CollTown::whereIn('cid', $town)->pluck('name', 'cid')->toArray();
        }

        $arr = $areaName + $townName;
        $str = '';
        //遍历地区
        foreach ($this->jobarea as $item) {
            if ($item != 0 && array_key_exists($item, $arr)) {
                $str .= $arr[$item] . ',';
            }
        }

        $temp = rtrim($str, ',');
        return strlen($temp) < 1 ? '地区不限' : $temp;
    }

    public function getBirthdayNameAttribute()
    {
        return $this->birthday ? substr($this->attributes['birthday'], 0, 7) : '';
    }

    /** 头像
     * @param $value
     * @return string
     */
    public function getAvatarAttribute($value)
    {
        return $value ? 'personalavatar/' . $value : '';
    }

    /** 更新迅搜状态
     * @param $id
     */
    public static function updateXSStatus($id)
    {
        if ($id) {
            Resume::where(['id' => $id, 'xs_status' => 1])
                ->update(['xs_status' => 0]);
        }
    }

    public function getIsexpectedsalaryAttribute($v)
    {
        return (is_null($v) || $v ==0) ? '0' : '1';
    }
}
